package com.qzsf.backend.controller;

import com.qzsf.backend.common.Result;
import com.qzsf.backend.service.FileService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
/*
 * @Author: Dennis Chen
 * @CreateTime: 2024/10/31 3:10 周四
 * @Email 1429278462@qq.com
 * @Remark 文件传输阿里oss控制器
 * @Version 1.0
 */
@RestController
@RequestMapping("files")
public class FileController {
    @Autowired
    private FileService fileService;

    @PostMapping
    public Result updateFile(@RequestParam MultipartFile file) {
        try {
            String url = fileService.uploadFile(file);
            return Result.success(url);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.error("图片上传失败，IO异常: " + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("图片上传失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/{objectName}")
    public Result deleteFile(@PathVariable String objectName) {
        System.out.println(objectName);
        try {
            fileService.deleteFile(objectName);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("图片删除失败");
        }
    }
}
